/**
 * Created by Amaris on 2016/10/9.
 */

var Waterline = require('waterline');
var uuid = require('node-uuid');
var moment = require('moment');

/**
 * 用户表
 * @type {void|*}
 */
module.exports = Waterline.Collection.extend({
    identity: 'user',
    tableName: 'tb_user',
    connection: "mysql",
    attributes: {

        rowId: {
            type: 'string',
            primaryKey: true,
            unique: true,
            required: true,
            columnName: 'rowid',
            defaultsTo: function () {
                return uuid.v4();
            }
        },
        name: {
            type: 'string',
            required: true,
            columnName: 'name'
        },
        password: {
            type: 'string',
            required: true,
            columnName: 'password'
        },
        weid: {
            type: 'string',
            required: true,
            columnName: 'weid'
        },
        /**
         * 用户是否可用，1表示可用，0表示是通讯录用户，但是不可以登录，-1表示已删除通讯录用户
         */
        enable: {
            type: 'string',
            required: true,
            columnName: 'enable'
        },
        phone: {
            type: 'string',
            unique: true,
            columnName: 'phone'
        },
        tel: {
            type: 'string',
            columnName: 'tel'
        },
        email: {
            type: 'string',
            columnName: 'email'
        },
        des: {
            type: 'string',
            columnName: 'des'
        },
        createUid: {
            type: 'string',
            required: true,
            columnName: 'createuid'
        },
        /**
         *2017年06月19日14:14:41  新增status、pinyi、acronym字段
         */
        status: {
            type: 'string',
            required: true,
            columnName: 'status'
        },
        pinyi: {
            type: 'string',
            required: true,
            columnName: 'pinyi'
        },
        acronym: {
            type: 'string',
            required: true,
            columnName: 'acronym'
        },
        // /**用户角色等级，0普通微信用户，1可发布内容的正常用户，2可打分的用户，3管理员**/
        // level: {
        //     type: 'string',
        //     required: true,
        //     columnName: 'level'
        // },
        /**用户角色等级，0普通微信用户，1可发布内容的正常用户，2可打分的用户，3管理员**/
        role: {
            type: 'string',
            required: true,
            columnName: 'role'
        },

        extension: {
            type: 'string',
            columnName: 'extension'

        },
        des: {
            type: 'string',
            columnName: 'des'

        },
        industry: {
            type: 'string',
            columnName: 'industry'

        },
        department: {
            type: 'string',
            columnName: 'department'

        },
        position: {
            type: 'string',
            columnName: 'position'
        },
        seller: {
            type: 'string',
            required: true,
            columnName: 'seller',
            model: 'seller'
        },
        surveying: {
            collection: 'surveying',
            via: 'createUid'
        },
        toJSON: function () {
            var obj = this.toObject();
            obj.createdAt = moment(obj.createdAt).format('YYYY/MM/DD hh:mm');
            obj.updatedAt = moment(obj.updatedAt).format('YYYY/MM/DD hh:mm');
            obj.password = null;
            return obj;
        }

    }
});
